package com.redoor.app.stake.basicdata.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.redoor.app.stake.auth.commons.StakeApplicationStatus;
import com.redoor.app.stake.basicdata.model.BaseVendorInfo;
import com.redoor.app.stake.basicdata.model.controllerModel.BaseVendorInfoActionBean;
import com.redoor.app.stake.basicdata.model.controllerModel.BaseVendorInfoCriteria;
import com.redoor.app.stake.basicdata.service.BaseVendorInfoService;
import com.redoornetwork.framework.core.response.ResponseResult;
import com.redoornetwork.framework.core.response.ResultGenerator;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
* Created by wangjunlei on 2018-03-12 17:30:28.
*/
@RestController
@RequestMapping("/base/vendor/infos")
public class BaseVendorInfoController {

    private Logger logger = LoggerFactory.getLogger(BaseVendorInfoController.class);

    @Autowired
    private BaseVendorInfoService baseVendorInfoService;

    @ApiOperation(value = "添加厂商信息")
    @RequiresPermissions("base:vender:insert")
    @PostMapping("/add")
    public ResponseResult add(BaseVendorInfoCriteria baseVendorInfoCriteria) {
        try {
            int status = baseVendorInfoService.saveVendorInfo(baseVendorInfoCriteria);
            //判断添加语句是否执行,如果执行则大于0,返回成功
            if(status > 0){
                logger.info("厂商添加成功");
                return ResultGenerator.generatorSuccess();
            }else {
                return ResultGenerator.generatorFail(StakeApplicationStatus.OTHER_EXCEPTION);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.generatorFail(StakeApplicationStatus.OTHER_EXCEPTION);
        }
    }

    /**
     * 状态删除厂商信息
     * @Author zhangYiHang
     * @Date 2018/3/21 9:23
     */
    @ApiOperation(value = "厂商信息删除,传入参数为厂商uuid='主键'")
    @RequiresPermissions("base:vender:delete")
    @PostMapping("/del")
    public ResponseResult delete(String uuid) {
        try {
            int status = baseVendorInfoService.updateStatus(uuid);
            //判断修改语句是否执行,如果执行则大于0,返回成功
            if (status > 0) {
                logger.info("厂商删除成功");
                return ResultGenerator.generatorSuccess();
            } else {
                return ResultGenerator.generatorFail(StakeApplicationStatus.OTHER_EXCEPTION);
            }
        } catch (Exception e) {
            logger.error("厂商删除异常,主键:" + uuid);
            return ResultGenerator.generatorFail(StakeApplicationStatus.OTHER_EXCEPTION);
        }
    }

    /**
     * 厂商信息修改
     * @Author zhangYiHang
     * @Date 2018/3/21 10:01
     */
    @ApiOperation(value = "厂商信息修改")
    @RequiresPermissions("base:vender:update")
    @PostMapping("/update")
    public ResponseResult update(BaseVendorInfoCriteria baseVendorInfoCriteria) {
        try {
            int status = baseVendorInfoService.updateVendorInfo(baseVendorInfoCriteria);
            //判断修改语句是否执行,如果执行则大于0,返回成功
            if (status > 0) {
                logger.info("厂商修改成功");
                return ResultGenerator.generatorSuccess();
            } else {
                return ResultGenerator.generatorFail(StakeApplicationStatus.OTHER_EXCEPTION);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.generatorFail(StakeApplicationStatus.OTHER_EXCEPTION);

        }
    }
    @ApiOperation(value = "根据传入主键uuid查询对象,uuid='主键'")
    @PostMapping("/detail")
    public ResponseResult detail(String uuid) {
        BaseVendorInfo baseVendorInfo = baseVendorInfoService.findById(uuid);
        return ResultGenerator.generatorSuccess(baseVendorInfo);
    }
    @ApiOperation(value = "厂商信息列表展示")
    @RequiresPermissions("base:vender:list")
    @GetMapping("/list")
    public ResponseResult list(BaseVendorInfoActionBean baseVendorInfoActionBean) {
        List<BaseVendorInfo> list = baseVendorInfoService.qryVendorInfo(baseVendorInfoActionBean);
        Long total = baseVendorInfoService.qryVendorInfoTotal(baseVendorInfoActionBean);
        PageInfo pageInfo = new PageInfo(list);
        pageInfo.setTotal(total);

        return ResultGenerator.generatorSuccess(pageInfo);
    }
    @ApiOperation(value = "根据名称或编号查询对象是否存在,0存在,其他不存在")
    @PostMapping("/qryVendorInfoByNameOrVendorNo")
    public ResponseResult qryVendorInfoByNameOrVendorNo(String name,String vendorNo){
        Map<String, Integer> map = baseVendorInfoService.qryVendorInfoByNameOrVendorNo(name,vendorNo);
        return ResultGenerator.generatorSuccess(map);
    }

}
